#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
using LL = long long;
const int N = 1000010;
int a[N];
LL c[N];
int n;
LL sum = 0, avg;

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i], sum += a[i];
    avg = sum / n;
    for (int i = 1; i <= n; ++i)
        c[i] = c[i - 1] + a[i] - avg;
    sort(c + 1, c + n + 1);
    int mid = c[(n >> 1) + 1];
    LL ans = 0;
    for (int i = 1; i <= n; ++i)
        ans += abs(mid - c[i]);
    cout << ans << endl;
    return 0;
}